package com.xingkeduo.business.transformation.json;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.query.JobseekerQO;
import com.xingkeduo.pojo.table.JobseekerTO;
import com.xingkeduo.pojo.table.JobseekerTO.Status;
import com.xingkeduo.pojo.table.JobseekerTO.Type;
import com.xingkeduo.pojo.utils.Base64Utils;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.RecruitSourceEnum;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * 求职者账户封装类
 *
 * @author 高杰
 * @time 2018年12月25日 下午1:45:17
 * @description
 */
@Component
public class JobseekerJTO extends BaseJson {
    public static JobseekerTO createTableForNew(String json) throws Throwable {
        JobseekerTO table = new JobseekerTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setLogin(validLogin(root, YesNoEnum.NO)).setPassword(validPassword(root, YesNoEnum.NO)).setWechatName(validString(root, "wechatName", 100, YesNoEnum.YES));

        return table;
    }

    public static JobseekerTO createTableForModify(String json) throws Throwable {
        JobseekerTO table = new JobseekerTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setPassword(validPassword(root, YesNoEnum.NO)).setStatus(validStatus(root, YesNoEnum.NO));

        return table;
    }

    public static JobseekerTO createTableForReset(String json) throws Throwable {
        JobseekerTO table = new JobseekerTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        table.setStatus(validStatus(root, YesNoEnum.YES));

        return table;
    }

    public static JobseekerQO createQueryForAssess(String json) throws Throwable {
        JobseekerQO query = new JobseekerQO();
        JsonNode root = validJson(json);
        if (root == null)
            return query;

        query.setStatuses(validStatuses(root, YesNoEnum.NO)).setType(Type.HAIRCUT);
        if (query.getStatuses() == null)
            query.setStatus(validStatus(root, YesNoEnum.YES));

        return query;
    }

    public static JobseekerQO createQueryForPrimary(String json) throws Throwable {
        JobseekerQO query = new JobseekerQO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        query.setId(validId(root, YesNoEnum.YES));
        return query;
    }

    public static ObjectNode toJson(JobseekerTO table) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();

        if (table != null) {
            if (table.getLogin() != null)
                root.put("login", table.getLogin());
            if (table.getWechatName() != null)
                root.put("wechatName", Base64Utils.decoderParam(table.getWechatName()));
            if (table.getType() != null) {
                root.put("type_value", table.getType().getValue());
                root.put("type_desc", table.getType().getDesc());
            }
            if (table.getStatus() != null) {
                root.put("status_value", table.getStatus().getValue());
                root.put("status_desc", table.getStatus().getDesc());
            }

            toBaseJson(root, table);
            //面试主信息
            if (table.getJob() != null)
                root.putPOJO("job", JobseekerJobJTO.toJson(table.getJob()));
            if (table.getSocial() != null)
                root.putPOJO("social", JobseekerSocialJTO.toJson(table.getSocial()));
            if (!CommonUtil.isEmpty(table.getAssesses()))
                root.putPOJO("assesses", JobseekerAssessJTO.toJson(table.getAssesses()));
            if (table.getLastFirstExam() != null)
                root.putPOJO("lastfirstexam", JobseekerAssessJTO.toJson(table.getLastFirstExam()));
            if (table.getLastSecondExam() != null)
                root.putPOJO("lastsecondexam", JobseekerAssessJTO.toJson(table.getLastSecondExam()));
            if (null != table.getJobDetailList()) {
                root.putPOJO("detail", JobseekerJobDetailJTO.toJson(table.getJobDetailList()));
            }
            //求职者面试记录
            if (null != table.getAssessList()) {
                root.putPOJO("faceList", JobseekerAssessJTO.toJson(table.getAssessList()));
            }
            //培训信息
            if (null != table.getAssessTO()) {
                root.putPOJO("assessTO",JobseekerAssessJTO.toJson(table.getAssessTO()));
            }
            //新增人才库评分
            if(null != table.getJobseekerScore()){
                root.putPOJO("jobseekerScore", JSON.toJSON(table.getJobseekerScore()));
            }
        }

        return root;
    }

    public static ArrayNode toJson(List<JobseekerTO> tables) throws Throwable {
        ArrayNode root = MAPPER.createArrayNode();

        if (!CommonUtil.isEmpty(tables))
            for (Iterator<JobseekerTO> iterator = tables.iterator(); iterator.hasNext(); ) {
                JobseekerTO table = iterator.next();
                root.add(toJson(table));
            }

        return root;
    }

    //招聘渠道
    public static ArrayNode toJsonOfRecruitSourceEnum() throws Throwable {
        ArrayNode root = MAPPER.createArrayNode();

        for (RecruitSourceEnum e : RecruitSourceEnum.values()) {
            ObjectNode o = MAPPER.createObjectNode();
            o.put("value", e.getValue());
            o.put("name", e.getDesc());
            root.add(o);
        }

        return root;
    }


    public static String validLogin(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "login";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value) || value.length() > 50)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static String validPassword(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "password";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (CommonUtil.isEmpty(value) || value.length() > 32)
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return value;
        }
        return null;
    }

    public static Status validStatus(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "status";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            String value = node.path(key).asText().trim();
            if (!Status.validValue(value))
                throw new InputParamException(InputParamException.Type.INVALID, key + " error");
            return Status.getStatus(value);
        }
        return null;
    }

    public static Set<Status> validStatuses(JsonNode node, YesNoEnum isRequired) throws Throwable {
        String key = "statuses";
        if (YesNoEnum.YES == isRequired && !node.has(key))
            throw new InputParamException(InputParamException.Type.NULL, key);

        if (node.has(key)) {
            Set<Status> statuses = new HashSet<>();
            String value = node.path(key).asText().trim();
            String[] values = value.split(",");
            for (int i = 0; i < values.length; i++) {
                if (!Status.validValue(values[i]))
                    throw new InputParamException(InputParamException.Type.INVALID, key + " error");
                statuses.add(Status.getStatus(values[i]));
            }
            return statuses;
        }
        return null;
    }
}
